#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: data_sync_source_node.py 
@version:
@time: 2022/03/23 
"""
from TapClientCore.T04_logic.Dag02.node.node_base import NodeBase


class DataSyncSinkNode(NodeBase):
    """
    {
  "id": "8d7faff7-e3bf-4b97-a3bd-6381e7d846d2",
  "name": "new",
  "type": "table",
  "attrs": {
    "position": [
      -106,
      191.813
    ],
    "connectionType": "source_and_target"
  },
  "tableName": "new",
  "databaseType": "tidb",
  "connectionId": "621df3b198bcdf1ea2e2251c",
  "existDataProcessMode": "keepData",
  "writeStrategy": "updateOrInsert",
  "updateConditionFields": [
    "POLICY_ID"
  ]
}
    """
    node = None

    def __init__(self, datasource_name):
        self.generate_node(datasource_name)

    def generate_node(self, datasource_name):
        """
        {
            name: '',
            type: '',
            tableName: '',
          "existDataProcessMode": "keepData",
          "writeStrategy": "updateOrInsert",
          "updateConditionFields": [
            "POLICY_ID"
          ]
        }
        :param datasource_name:
        :return:
        """
        from TapClientCore.T05_TM_feature.v2.TMF01_datasource.tmf_http import TMFHttp
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_SYNC_DAG_NODE_TARGET
        TMF = TMFHttp()
        response = TMF.get_data_source_by_name(datasource_name)
        node = {
            'id': self.newId,
            'attrs': {
                'position': [
                    0,
                    200
                ],
                'connectionName': datasource_name,
                'connectionType': response.get('connection_type'),
                'pdkHash': response.get('pdkHash'),
                'pdkType': 'pdk',
            },
            'databaseType': response.get('database_type'),
            'connectionId': response.get('id')
        }
        tmp = TASK_CREATE_DATA_SYNC_DAG_NODE_TARGET.copy()
        tmp.update(node)
        self.node = tmp


if __name__ == '__main__':
    T = DataSyncSinkNode('AUTO_TIDB_SOURCE')
    print(T.node)